package graph;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class WeightedDirectedGraph<O, W> {

	Set<Vertex<O, W>> vertices = new HashSet<Vertex<O, W>>();

	public WeightedDirectedGraph() {
		// TODO
	}

	public void putVertex(O object) {
		vertices.add(new Vertex<O, W>(this, object));
	}

	public void addEdge(W weight, O object1, O object2) {
		Vertex<O, W> vertex1 = getVertex(object1);
		Vertex<O, W> vertex2 = getVertex(object2);
		if (vertex1 != null && vertex2 != null) {
			vertex1.addSide(vertex2, weight);
			vertex2.addSide(vertex1, weight);
		}
	}

	public W getWeight(O object1, O object2) {
		Vertex<O, W> vertex1 = getVertex(object1);
		Vertex<O, W> vertex2 = getVertex(object2);
		if (vertex1 != null && vertex2 != null) {
			return vertex1.getWeight(vertex2.getObject());
		}
		return null;
	}

	public Vertex<O, W> getVertex(O object) {
		for (Vertex<O, W> vertex : vertices) {
			if (vertex.getObject().equals(object)) {
				return vertex;
			}
		}
		return null;
	}

	public String toString() {
		String returnString = "";
		for (Vertex<O, W> vertex : vertices) {
			returnString += vertex.toString() + "\n";
		}
		return returnString;
	}

}
